package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.ShopTypeInfoMapper;
import com.guigu.pojo.ShopInfo;
import com.guigu.pojo.ShopTypeInfo;
import com.guigu.service.ShopInfoService;
import com.guigu.service.ShopTypeInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopTypeInfoServiceImpl extends ServiceImpl<ShopTypeInfoMapper,ShopTypeInfo>
implements ShopTypeInfoService {
    @Autowired
    ShopTypeInfoMapper shopTypeInfoMapper; //商品分类Mapper
    @Autowired
    ShopInfoService shopInfoService;//商品Service
    //获取所有的分类
    @Override
    public List<ShopTypeInfo> queryAllShopType() {
        QueryWrapper<ShopTypeInfo> queryWrapper=new QueryWrapper<ShopTypeInfo>();
        queryWrapper.eq("is_delete",0);
        return  shopTypeInfoMapper.selectList(queryWrapper);
    }

    //添加分类
    @Override
    public Map addShopType(ShopTypeInfo shopType) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","创建失败");
        shopType.setIsDelete(0);
        boolean save = this.save(shopType);
        if (save){
            map.put("code",1);
            map.put("msg","创建成功");
        }
        return map;
    }

    //分类分页
    @Override
    public Page<ShopTypeInfo> selectAllShopType(ShopTypeInfo shopType, Integer pageno, Integer pagesize) {
        QueryWrapper<ShopTypeInfo> queryWrapper=new QueryWrapper<ShopTypeInfo>();
        queryWrapper.eq("is_delete",0);
        if (StringUtils.isNotBlank(shopType.getName())){
            queryWrapper.like("name",shopType.getName());
        }
        return this.page(new Page<ShopTypeInfo>(pageno,pagesize),queryWrapper);
    }

    //根据id查询分类对象
    @Override
    public ShopTypeInfo queryTypeById(Integer id) {
        return this.getById(id);
    }
    //编辑
    @Override
    public Map updateShopTypeById(ShopTypeInfo shopType) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","编辑失败");
        boolean b = this.updateById(shopType);
        if (b){
            map.put("code",1);
            map.put("msg","编辑成功");
        }
        return map;
    }
    //删除
    @Override
    public Map deleteShopTypeById(Integer id) {
        //删除前查询是否还有该商品的分类
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","删除失败");
        QueryWrapper<ShopInfo> queryWrapper=
                new QueryWrapper<ShopInfo>();
        queryWrapper.eq("shop_type",id);
        List<ShopInfo> list = shopInfoService.list(queryWrapper);
        if (list.size()>0){
            map.put("msg","还有商品使用该分类,请先确认再删除");
        }else {
            ShopTypeInfo type=new ShopTypeInfo();
            type.setId(id);
            type.setIsDelete(1);
            boolean b = this.updateById(type);
            if (b){
                map.put("code",1);
                map.put("msg","删除成功");
            }
        }
        return map;
    }

    //验证分类名称是否重复
    @Override
    public Map ckShopTypeByName(String name) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","名称重复请重新输入");
        QueryWrapper<ShopTypeInfo> queryWrapper
                =new QueryWrapper<ShopTypeInfo>();
        queryWrapper.eq("name",name);
        ShopTypeInfo one = this.getOne(queryWrapper);
        if (one==null){
            map.put("code",1);
        }
        return map;
    }


}
